package com.ayun.member.controller;

import com.ayun.base.Result;
import com.ayun.member.entity.Member;
import com.ayun.member.req.MemberREQ;
import com.ayun.member.service.IMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 斌叔
 * @description: TODO
 * @create 2022-05-08 11:33
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private IMemberService memberService;



    // 分页查询会员
    @PostMapping("/list/search/{page}/{size}")
    public Result search(@PathVariable("page") long page,
                         @PathVariable("size") long size,
                         @RequestBody(required = false) MemberREQ req) {
        logger.info("分页查询会员列表：page={}，size={}", page, size);
        return memberService.search(page, size, req);
    }

    @GetMapping("/list")
    public Result list() {
        logger.info("查询会员列表");
        return Result.ok();
    }

    //新增会员
    @PostMapping("/add")
    public Result add(@RequestBody Member req) {
        logger.info("新增会员：{}", req);
        Boolean b = memberService.save(req);
        if (b) {
            return Result.ok();
        }
        return Result.error("新增会员失败");
    }

    /**
     * @Description:  删除会员
     * @param: null
     * @return * @return null
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") int id) {
        logger.info("删除会员：{}", id);
        Boolean b = memberService.removeById(id);
        if (b) {
            return Result.ok();
        }
        return Result.error("删除会员失败");
    }


    /**
     * @Description:  查询会员详情id
     * @param: null
     * @return * @return null
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") int id) {
        logger.info("查询会员详情：{}", id);
        Member member = memberService.getById(id);
        return Result.ok(member);
    }

    /**
     * @Description:  修改会员
     * @param: null
     * @return * @return null
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") int id, @RequestBody Member req) {
        logger.info("修改会员：{}", req);
        return memberService.update(id, req);
    }

}

